#include "RocketPlaneStrangtegy.h"
#include "Config.h"
#include "WeaponBank.h"
#include "Bullet.h"
#include "MathUtil.h"
#include "RocketExtendProcessor.h"

#define ANGLE_OPEN 90
#define ACCELERATION_FACETOR 1.5f

RocketPlaneStrangtegy::RocketPlaneStrangtegy(void)
{
	_weapon = NULL;
	_timeDelay = 1;
	_numRocket = 4;
	_damage = 10;
}


RocketPlaneStrangtegy::~RocketPlaneStrangtegy(void)
{
	CC_SAFE_RELEASE(_weapon);
}

void RocketPlaneStrangtegy::onEnter()
{
	BaseStrategy::onEnter();
	schedule(SEL_SCHEDULE(&RocketPlaneStrangtegy::shoot), _timeDelay);
}

void RocketPlaneStrangtegy::onExit()
{
	BaseStrategy::onExit();
	unschedule(SEL_SCHEDULE(&RocketPlaneStrangtegy::shoot));
}

bool RocketPlaneStrangtegy::init()
{
	if (!BaseStrategy::init())
		return false;
	setWeapon(WeaponBank::sharedWeaponBank()->getWeapon(WEAPON_ROCKET));
	addChild(_weapon);
	return true;
}

void RocketPlaneStrangtegy::shoot(float dt)
{
	CCPoint _positionShoot = ccp(getOwner()->boundingBox().size.width/2, getOwner()->boundingBox().size.height/2);
	float deltaAngle = (_numRocket == 1)?0:ANGLE_OPEN / (_numRocket - 1);
	float startAngle = (_numRocket == 1)?0:ANGLE_OPEN / 2;


	for (int i = 0; i < _numRocket; i++)
	{
		float angle = startAngle - i * deltaAngle;

		Bullet* bullet = dynamic_cast<Bullet*>(_weapon->addBullet()->objectAtIndex(0));
		CCPoint bVel = bullet->getVelocity();
		bullet->setVelocity(ccp(0, 1));
		bullet->setIsRotateByDirect(false);
		bullet->setVelocity(bVel);
		bullet->setPosition(getOwner()->getPosition() + _positionShoot + bullet->getPosition());
		CCPoint vel = MathUtil::rotateVector(bullet->getVelocity(), MathUtil::DegreeToRad(angle));
		bullet->setVelocity(vel);
		bullet->setAcceleration(vel * -ACCELERATION_FACETOR);
		RocketExtendProcessor* extend = RocketExtendProcessor::create();
		bullet->addChild(extend);
	}
}
